<template>
<div>
  <nav-header></nav-header>
  <nav-bread>
    <span>后台管理</span>
  </nav-bread>
 <el-container style="height: 500px; border: 1px solid #eee">
  <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
    <el-menu :default-openeds="['1']">
      <el-submenu index="1">
        <template slot="title"><i class="el-icon-message"></i>系统后台导航</template>
        <el-menu-item-group>
          <template slot="title">管理分类</template>
          <el-menu-item v-for="(item,index) of subTitleList" :key= index
           @click="changeTarget(index)">{{item}}</el-menu-item>
          
        </el-menu-item-group>
        
      </el-submenu>
      
    </el-menu>
  </el-aside>
  
  <el-container >
    <el-header style="text-align: right; font-size: 12px" v-show= !flag>
      <a href="javascript:;" style="margin-right: 15px; color:#fff" @click="handleAdd">添加商品</a>
    </el-header>
    
    <el-main v-show= flag>
      <el-table :data="userList" >
        <el-table-column  label="用户ID" width="180">
          <template slot-scope= "scope">
            <span>{{scope.row.userId}}</span>
          </template>
           
        </el-table-column>
        <el-table-column  label="姓名" width="180">
           <template slot-scope= "scope">
            <span>{{scope.row.userName}}</span>
          </template>
        </el-table-column>
        <el-table-column  label="密码" width="180">
           <template slot-scope= "scope">
            <span>{{scope.row.userPwd}}</span>
          </template>
        </el-table-column>
        
        <el-table-column  label="操作">
           <template slot-scope= "scope">
            <el-button size="mini" type="danger" @click="handleDel(scope.$index,scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>

    <!-- 商品列表 -->

    <el-main v-show= !flag>
      <el-table :data="goodsList" >
        <el-table-column  label="商品Id" width="180">
          <template slot-scope= "scope">
            <span>{{scope.row.productId}}</span>
          </template>
        </el-table-column>
         <el-table-column  label="商品图片"  width= "180">
           <template slot-scope= "scope">
          
            <img :src= scope.row.productImage alt="" height= 50 rem>
          </template>
        </el-table-column>
        <el-table-column  label="商品名" width="180">
           <template slot-scope= "scope">
            <span>{{scope.row.productName}}</span>
          </template>
        </el-table-column>
        <el-table-column  label="售价" width="180">
           <template slot-scope= "scope">
            <span>{{scope.row.salePrice}}</span>
          </template>
        </el-table-column>
       <el-table-column  label="数量" width="180">
           <template slot-scope= "scope">
            <span>{{scope.row.productNum}}</span>
          </template>
        </el-table-column>
        <el-table-column  label="操作">
           <template slot-scope= "scope">
             <el-button size="mini"  @click="handleChange(scope.$index,scope.row)">修改</el-button>
             <el-button size="mini" type="danger" @click="handleGoods(scope.$index,scope.row)">删除</el-button>
          </template>
    
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>
</el-container>

<!-- 添加商品时的模态框 -->
  <Modal :mdShow="modalInfo" @close="closeModal">
      <p slot="message">
        <input  v-model='productName'  type="text" placeholder="商品名" >
        <input v-model='salePrice' type="text" placeholder="商品价格" >
        <input v-model='productImage' type="text" placeholder="商品图片" >
        <input v-model='productNum' type="text" placeholder="商品数量" >
      </p>

      <div slot="btnGroup">
        <a class="btn btn--m" href="javascript:;" @click="addGoods">确认</a>
        <a class="btn btn--m btn--red" href="javascript:;" @click="modalInfo = false">关闭</a>
      </div>
    </Modal>
<!-- 修改商品时的模态框 -->
     <Modal :mdShow="modalChange" @close="closeModal">
      <p slot="message">
        <input v-model='salePrice' type="text" placeholder="商品价格为" >
        <input v-model='productNum' type="text" placeholder="商品数量为" >
      </p>

      <div slot="btnGroup">
        <a class="btn btn--m" href="javascript:;" @click="changeGoods()">确认</a>
        <a class="btn btn--m btn--red" href="javascript:;" @click="modalChange = false">关闭</a>
      </div>
    </Modal>
  <nav-footer></nav-footer>
</div>

</template>
<script>
  import NavHeader from './../components/NavHeader'
  import NavFooter from './../components/NavFooter'
  import NavBread from './../components/NavBread'
  import Modal from "./../components/Modal";
  import axios from 'axios'
export default {
  data(){
    return{
      subTitleList:['用户管理','商品管理'],
      userList:[],
      flag: true,
      goodsList:[],
      modalInfo: false,
      productName:'',
      salePrice:'',
      productImage:'',
      productNum:'',
      modalChange: false,
      productId:''
    }
  },
  components:{
    NavHeader,
    NavFooter,
    NavBread,
    Modal
  },
  mounted(){
    this.init(),
    this.init2()
  },
  methods:{
    init(){
      axios.get('/users').then((response) => {
        let res = response.data;
        if(res.status=='0'){
          this.userList = res.result;
          }else{
            this.userList = []
          }
      })
    },
    changeTarget(index){
     if(index=='0'){
       this.flag= true;
     }else{
       this.flag= false;
     }
    
    },
    init2(){
     axios.get('/goods').then( (response)=>{
           let res = response.data;
           if(res.status=='0'){
              this.goodsList= res.result;
           }else{
             this.goodsList=[];
           }
        })
    },
    handleDel(index,row){
     axios.post('/users/deluser',{
       userId:row.userId
     }).then((response)=>{
        let res= response.data;
        if(res.status=='0'){
          this.init();
        }else{
          console.log('删除失败')
        }
     })
    },
    handleGoods(index,row){
      axios.post('/goods/delgoods',{
        productId:row.productId
      }).then((response)=>{
        let res= response.data;
        if(res.status=='0'){
          this.init2();
        }else{
          console.log('删除失败')
        }
     })
    },
    handleAdd(){
      this.modalInfo = true;
    },
    addGoods(){
      axios.post('/goods/addgoods',{
        productName: this.productName,
        productImage: this.productImage,
        salePrice: this.salePrice,
        productNum: this.productNum
      }).then( (response) => {
          let res = response.data;
          if(res.status=='0'){
            this.modalInfo= false;
            this.init2();
          }else{
            console.log("商品加入失败")
          }
      })
    },
    handleChange(index,row){
     this.productId = row.productId
     this.modalChange= true;
    },
    changeGoods(){
      axios.post('/goods/changegoods',{
        productId: this.productId,
        salePrice: this.salePrice,
        productNum: this.productNum
      }).then((response) => {
          let res = response.data;
          if(res.status=='0'){
            this.modalChange= false;
            this.init2();
          }else{
            console.log("商品修改失败")
          }
      })
    },
    closeModal() {
     this.modalInfo= false;
     this.modalChange= false;
    },
  }
}
</script>
<style>
  .el-header {
    background-color: #B3C0D1;
    color: #333;
    line-height: 60px;
  }
  
  .el-aside {
    color: #333;
  }
</style>